{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "89548a9b-ecb7-413d-9286-090de5cb34d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resolving Hex dependencies...\n",
      "Dependency resolution completed:\n",
      "Unchanged:\n",
      "  certifi 2.8.0\n",
      "  dotenv 3.0.0\n",
      "  hackney 1.18.0\n",
      "  idna 6.1.1\n",
      "  metrics 1.0.1\n",
      "  mime 1.6.0\n",
      "  mimerl 1.2.0\n",
      "  parse_trans 3.3.1\n",
      "  ssl_verify_fun 1.1.6\n",
      "  tesla 1.4.3\n",
      "  unicode_util_compat 0.7.0\n",
      "All dependencies are up to date\n",
      "===> Analyzing applications...\n",
      "===> Compiling parse_trans\n",
      "===> Analyzing applications...\n",
      "===> Compiling mimerl\n",
      "===> Analyzing applications...\n",
      "===> Compiling metrics\n",
      "===> Analyzing applications...\n",
      "===> Compiling unicode_util_compat\n",
      "===> Analyzing applications...\n",
      "===> Compiling idna\n",
      "===> Analyzing applications...\n",
      "===> Compiling certifi\n",
      "===> Analyzing applications...\n",
      "===> Compiling hackney\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       ":ok"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Boyle.activate(\"aoc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c3af5504-1409-4589-8fea-5b5671690e27",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{:module, AoC.IO, <<70, 79, 82, 49, 0, 0, 11, 8, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 13, 0, 0, 0, 29, 13, 69, 108, 105, 120, 105, 114, 46, 65, 111, 67, 46, 73, 79, 8, 95, 95, 105, 110, 102, 111, 95, 95, ...>>, {:submit_answer, 5}}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "defmodule AoC.Client do\n",
    "    use Tesla\n",
    "\n",
    "    plug Tesla.Middleware.FormUrlencoded\n",
    "end\n",
    "\n",
    "defmodule AoC.IO do\n",
    "    def get_input(year, day, session) do\n",
    "        {:ok, response} = AoC.Client.get(\n",
    "            \"https://adventofcode.com/#{year}/day/#{day}/input\",\n",
    "            headers: [{\"cookie\", \"session=#{session}\"}]\n",
    "        )\n",
    "        \n",
    "        response.body\n",
    "    end\n",
    "    \n",
    "    def submit_answer(year, day, level, session, answer) do\n",
    "        {:ok, response} = AoC.Client.post(\n",
    "            \"https://adventofcode.com/#{year}/day/#{day}/answer\",\n",
    "            %{level: level, answer: answer},\n",
    "            headers: [{\"cookie\", \"session=#{session}\"}]\n",
    "        )\n",
    "        \n",
    "        response.body\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73568ce9-592b-49fe-8e60-11a184cfcf57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year = 2021\n",
    "day = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "75c2cb94-4ebf-4905-b836-5cdc8b8cb8b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Input data fetched from adventofcode.com\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "env = Dotenv.load.values\n",
    "session = Map.get(env, \"AOC_SESSION\")\n",
    "input = AoC.IO.get_input(year, day, session)\n",
    "\"Input data fetched from adventofcode.com\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "da07ffab-aa9b-40af-896e-54ca871d220d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"199\\n200\\n208\\n210\\n200\\n207\\n240\\n269\\n260\\n263\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample = \"199\n",
    "200\n",
    "208\n",
    "210\n",
    "200\n",
    "207\n",
    "240\n",
    "269\n",
    "260\n",
    "263\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "76d83f00-f0ee-4df3-aec7-235cb82ac037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{:module, AoC, <<70, 79, 82, 49, 0, 0, 10, 64, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 35, 0, 0, 0, 33, 10, 69, 108, 105, 120, 105, 114, 46, 65, 111, 67, 8, 95, 95, 105, 110, 102, 111, 95, 95, 10, 97, 116, ...>>, {:solve, 2}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "defmodule AoC do\n",
    "    def part_one(input) do\n",
    "        solve(input, 1)\n",
    "    end\n",
    "    \n",
    "    def part_two(input) do\n",
    "        solve(input, 3)\n",
    "    end\n",
    "\n",
    "    defp solve(input, seg) do\n",
    "        nums = input |>\n",
    "            String.split(\"\\n\") |>\n",
    "            Enum.filter(&(&1 != \"\")) |>\n",
    "            Enum.map(&(String.to_integer(&1)))\n",
    "            \n",
    "        seg_sum = 1..seg - 1//1 |> Enum.reduce(nums, fn x, acc -> \n",
    "            Enum.zip(acc, Enum.drop(nums, x)) |>\n",
    "            Enum.map(&(Tuple.sum(&1)))\n",
    "        end)\n",
    "        \n",
    "        Enum.zip(seg_sum, Enum.drop(seg_sum, 1)) |>\n",
    "        Enum.count(fn {x, y} -> x < y end)\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0c81404f-d571-4fff-a3b6-0251113a1551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AoC.part_one(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "be2050fd-484e-4889-89cf-a513de0656f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1583"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_one_ans = AoC.part_one(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "125a4ce3-a50b-4123-8918-a76d78e9d6f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "AoC.IO.submit_answer(year, day, 1, session, part_one_ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7bd4560a-9621-4e56-b462-0f1038e7bd47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AoC.part_two(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "623be414-1b35-4784-9a8c-6c6178dd0ce8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1627"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part_two_ans = AoC.part_two(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6dcce994-2aed-4bbc-ae06-ea9fd093275f",
   "metadata": {},
   "outputs": [],
   "source": [
    "AoC.IO.submit_answer(year, day, 2, session, part_two_ans)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Elixir",
   "language": "Elixir",
   "name": "ielixir"
  },
  "language_info": {
   "codemirror_mode": "elixir",
   "file_extension": "ex",
   "mimetype": "text/x-elixir",
   "name": "elixir",
   "nbconvert_exporter": "",
   "pygments_lexer": "elixir",
   "version": "1.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
